﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Finances.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using System.Linq;

namespace mozhi.smarterp.Finance.Finances
{
    public class EndPeriodProcessResultRepository : EfCoreRepository<FinanceDbContext, EndPeriodProcessResult , Guid>, IEndPeriodProcessResultRepository
    {
        public EndPeriodProcessResultRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        public async Task<List<EndPeriodProcessResult>> FindAsync(EndPeriodProcessResultQuery query)
        {
            var dbQuery = query.IncludeDetail ? DbContext.Set<EndPeriodProcessResult>().Include(x => x.Items).AsNoTracking() : DbContext.Set<EndPeriodProcessResult>().AsNoTracking();

            var result = await dbQuery
                .WhereIf(query.Id.HasValue, x => x.Id == query.Id)
                .WhereIf(query.ItemId.HasValue, x => x.ItemId == query.ItemId)
                .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated)
                .WhereIf(query.Period != 0, x => x.Period == query.Period)
                .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
                .WhereIf(query.StartPeriod.HasValue, x => x.Period >= query.StartPeriod)
                .WhereIf(query.EndPeriod.HasValue, x => x.Period <= query.EndPeriod)
                .WhereIf(query.Type != default, x => x.Type == query.Type)
                .ToListAsync();

            return result;
        }


        public override async Task<EndPeriodProcessResult> GetAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            EndPeriodProcessResult result;

            if(includeDetails)
                result = await DbContext.Set<EndPeriodProcessResult>().Include(x=>x.Items).FirstOrDefaultAsync(x=>x.Id == id);
            else
                result = await DbContext.Set<EndPeriodProcessResult>().FirstOrDefaultAsync(x => x.Id == id);

            return result;
        }

        public override async Task<EndPeriodProcessResult> FindAsync(Guid itemId, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            EndPeriodProcessResult result;

            if (includeDetails)
                result = await DbContext.Set<EndPeriodProcessResult>().Include(x => x.Items).AsNoTracking().FirstOrDefaultAsync(x => x.ItemId == itemId);
            else
                result = await DbContext.Set<EndPeriodProcessResult>().AsNoTracking().FirstOrDefaultAsync(x => x.ItemId == itemId);

            return result;
        }

        public async Task<int> GetCountAsync(EndPeriodProcessResultQuery query)
        {
            var result = await DbContext.Set<EndPeriodProcessResult>().AsNoTracking()
                .WhereIf(query.Id.HasValue, x => x.Id == query.Id)
                .WhereIf(query.ItemId.HasValue, x => x.ItemId == query.ItemId)
                .WhereIf(query.Activated.HasValue, x => x.Activated == query.Activated)
                .WhereIf(query.Period != 0, x => x.Period == query.Period)
                .WhereIf(query.Ids != null && query.Ids.Count > 0, x => query.Ids.Contains(x.Id))
                .WhereIf(query.StartPeriod.HasValue, x => x.Period >= query.StartPeriod)
                .WhereIf(query.EndPeriod.HasValue, x => x.Period <= query.EndPeriod)
                .WhereIf(query.Type != default, x => x.Type == query.Type)
               .CountAsync();

            return result;
        }
    }
}
